package array;

public class Array<E> {

    private E[] data;
    private int size;

    /**
     * @param capacity
     */
    public Array(int capacity) {
        data = (E[]) new Object[capacity];
        size = 0;
    }

    /**
     * default array
     */
    public Array() {
        this(10);
    }

    /**
     * get array size
     *
     * @return
     */
    public int getSize() {
        return size;
    }

    /**
     * get array capacity
     *
     * @return
     */
    public int getCapacity() {
        return data.length;
    }

    /**
     * array is empty or not
     *
     * @return
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * add element to the array end
     *
     * @param element
     */
    public void addLast(E element) {

//        if(size == data.length){
//            throw new ArrayIndexOutOfBoundsException(element);
//        }
//
//        data[size] = element;
//        size++;

        add(size, element);
    }

    /**
     * @param element
     */
    public void addFirst(E element) {
        add(0, element);
    }

    /**
     * add a element index position to the array
     * @param index
     * @param element
     */
    public void add(int index, E element) {

        if (index < 0 || index > size) {
            throw new ArrayIndexOutOfBoundsException();
        }

        if (size == data.length) {
            resize(2 * data.length);
        }

        for (int i = size - 1; i >= index; i--) {
            data[i + 1] = data[i];
        }

        data[index] = element;
        ++this.size;

    }

    /**
     * @param index
     * @return
     */
    private E remove(int index) {
        if (index < 0 || index > size) {
            throw new ArrayIndexOutOfBoundsException();
        }
        E ret = data[index];
        for (int i = index + 1; i < size; i++) {
            data[i - 1] = data[i];
        }
        --this.size;
        // loitering objects != memory leak
        data[size] = null;

        if (size == data.length / 4 && data.length / 2 != 0) {
            resize(data.length / 2);
        }

        return ret;
    }

    /**
     * @return
     */
    public E removeFrist() {
        return remove(0);
    }

    /**
     * @return
     */
    public E removeLast() {
        return remove(size - 1);
    }

    /**
     * @param element
     */
    public void removeElement(int element) {
        int index = find(element);
        if (-1 != index) {
            remove(index);
        }
    }

    /**
     * @param index
     * @return
     */
    public E get(int index) {
        if (index < 0 || index > size) {
            throw new ArrayIndexOutOfBoundsException();
        }
        return data[index];
    }

    /**
     * 获取最后一个元素
     * @return
     */
    public E getLast(){
        return get(size - 1);
    }

    /**
     * 获取第一个元素
     * @return
     */
    public E getFirst(){
        return get(0);
    }

    /**
     * @param index
     * @param element
     */
    void set(int index, E element) {
        if (index < 0 || index > size) {
            throw new ArrayIndexOutOfBoundsException();
        }
        data[index] = element;
    }

    /**
     * @param element
     * @return
     */
    public boolean contains(int element) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(element)) {
                return true;
            }
        }
        return false;
    }

    /**
     * @param element
     * @return
     */
    private int find(int element) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(element)) {
                return i;
            }
        }
        return -1;
    }

    public void swap(int i,int j){

        if( i < 0 || j < 0 || i > size || j > size ){
            throw new IllegalArgumentException("Index is illegal");
        }

        E t = data[i];
        data[i] = data[j];
        data[j] = t;

    }


    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("Array: size = %d , capacity = %d\n", size, data.length));
        res.append('[');
        for (int i = 0; i < size; i++) {
            res.append(data[i]);
            if (i != size - 1) {
                res.append(",");
            }
        }
        res.append(']');
        return res.toString();
    }

    /**
     * @param newCapacity
     */
    private void resize(int newCapacity) {
        E[] newData = (E[]) new Object[newCapacity];
        System.arraycopy(data, 0, newData, 0, size);
        data = newData;
    }


}
